package com.boarsoft.boar.message.server.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.boarsoft.config.spring.SpringXmlListener;
import com.boarsoft.message.bean.TargetMQ;
import com.boarsoft.message.core.MessageContext;

public class TargetMQXmlListener extends SpringXmlListener implements ApplicationContextAware {
	@Autowired
	protected MessageContext messageContext;
	/** */
	protected ApplicationContext applicationContext;

	@Override
	public boolean onReady(String code) {
		if (applicationContext.containsBean(code)) {
			TargetMQ q = applicationContext.getBean(TargetMQ.class, code);
			messageContext.removeTargetQueue(q.getCode(), false);
		}
		if (!super.onReady(code)) {
			return false;
		}
		TargetMQ tq = applicationContext.getBean(TargetMQ.class, code);
		messageContext.addTargetQueue(tq.getCode(), tq);
		return true;
	}

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
}